from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime
from enum import Enum


# 通用响应
class Response(BaseModel):
    code: int = 0
    message: str = "ok"
    data: Optional[Any] = None


# 认证相关
class SendCodeRequest(BaseModel):
    phone: str = Field(..., description="手机号")
    purpose: str = Field(..., description="目的: register/login/reset")


class RegisterRequest(BaseModel):
    phone: str
    code: str
    nickname: Optional[str] = None


class LoginCodeRequest(BaseModel):
    phone: str
    code: str


class LoginPasswordRequest(BaseModel):
    phone: str
    password: str = Field(..., max_length=72, description="密码（最大72字节）")


class SetPasswordRequest(BaseModel):
    new_password: str = Field(..., min_length=6, max_length=72, description="新密码（6-72字节）")


class ChangePasswordRequest(BaseModel):
    old_password: str = Field(..., max_length=72, description="旧密码")
    new_password: str = Field(..., min_length=6, max_length=72, description="新密码（6-72字节）")


class ResetPasswordRequest(BaseModel):
    phone: str
    code: str
    new_password: str = Field(..., min_length=6, max_length=72, description="新密码（6-72字节）")


class TokenResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"


# 用户相关
class UserBase(BaseModel):
    phone: str
    nickname: Optional[str] = None
    avatar_url: Optional[str] = None


class UserResponse(BaseModel):
    id: int
    phone: str
    nickname: Optional[str]
    avatar_url: Optional[str]
    status: str
    created_at: datetime
    
    class Config:
        from_attributes = True


class UpdateUserRequest(BaseModel):
    nickname: Optional[str] = None
    avatar_url: Optional[str] = None


# Agent相关
class AgentBase(BaseModel):
    name: str
    description: Optional[str] = None
    is_hot: bool = False
    model_id: int
    category_id: Optional[int] = None
    params: Optional[Dict[str, Any]] = None
    role: Optional[Dict[str, Any]] = None
    rag_kb_id: Optional[int] = None
    enabled: bool = True


class AgentCreate(AgentBase):
    tool_ids: Optional[List[int]] = []


class AgentUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    is_hot: Optional[bool] = None
    model_id: Optional[int] = None
    category_id: Optional[int] = None
    params: Optional[Dict[str, Any]] = None
    role: Optional[Dict[str, Any]] = None
    rag_kb_id: Optional[int] = None
    enabled: Optional[bool] = None
    tool_ids: Optional[List[int]] = None


class AgentResponse(BaseModel):
    id: int
    name: str
    description: Optional[str]
    is_hot: bool
    model_id: int
    category_id: Optional[int]
    params: Optional[Dict[str, Any]]
    role: Optional[Dict[str, Any]]
    rag_kb_id: Optional[int]
    enabled: bool
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


# 会话相关
class SessionCreate(BaseModel):
    agent_id: int
    title: Optional[str] = "新会话"


class SessionUpdate(BaseModel):
    title: Optional[str] = None
    pinned: Optional[bool] = None


class SessionResponse(BaseModel):
    id: int
    user_id: int
    agent_id: int
    title: str
    pinned: bool
    status: str
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


# 消息相关
class MessageCreate(BaseModel):
    content: str


class MessageResponse(BaseModel):
    id: int
    session_id: int
    role: str
    content: Optional[str]
    tool_name: Optional[str]
    tool_args: Optional[Dict[str, Any]]
    token_usage: Optional[Dict[str, Any]]
    created_at: datetime
    
    class Config:
        from_attributes = True


# 模型相关
class ModelBase(BaseModel):
    name: str
    provider: str
    kind: str
    description: Optional[str] = None
    enabled: bool = True
    api_key: Optional[str] = None
    params: Optional[Dict[str, Any]] = None


class ModelCreate(ModelBase):
    pass


class ModelUpdate(BaseModel):
    name: Optional[str] = None
    provider: Optional[str] = None
    kind: Optional[str] = None
    description: Optional[str] = None
    enabled: Optional[bool] = None
    api_key: Optional[str] = None
    params: Optional[Dict[str, Any]] = None


class ModelResponse(BaseModel):
    id: int
    name: str
    provider: str
    kind: str
    description: Optional[str]
    enabled: bool
    params: Optional[Dict[str, Any]]
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


# 工具相关
class ToolBase(BaseModel):
    name: str
    type: str
    category: Optional[str] = None
    description: Optional[str] = None
    tags: Optional[List[str]] = []
    schema: Optional[Dict[str, Any]] = None
    auth: Optional[Dict[str, Any]] = None
    enabled: bool = True


class ToolCreate(ToolBase):
    pass


class ToolUpdate(BaseModel):
    name: Optional[str] = None
    type: Optional[str] = None
    category: Optional[str] = None
    description: Optional[str] = None
    tags: Optional[List[str]] = None
    schema: Optional[Dict[str, Any]] = None
    auth: Optional[Dict[str, Any]] = None
    enabled: Optional[bool] = None


class ToolResponse(BaseModel):
    id: int
    name: str
    type: str
    category: Optional[str]
    description: Optional[str]
    tags: Optional[List[str]]
    schema: Optional[Dict[str, Any]]
    enabled: bool
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


# 分类相关
class CategoryResponse(BaseModel):
    id: int
    name: str
    sort: int
    created_at: datetime
    
    class Config:
        from_attributes = True


# 管理员相关
class AdminLoginRequest(BaseModel):
    username: str
    password: str


class AdminLoginResponse(BaseModel):
    token: str
    username: str

